package com.baidu.sggc.controller;

import com.baidu.sggc.audit.extend.AuditExtendContext;
import com.baidu.sggc.audit.extend.GeneralMessage;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.baidu.sggc.core.annotation.AuditLog;
import com.baidu.sggc.data.resp.Page;
import com.baidu.sggc.entity.ResourceInfo;
import com.baidu.sggc.service.ResourceInfoService;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.baidu.sggc.core.common.R;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 资源信息表(ResourceInfo)表控制层
 *
 * @author ZengFz
 * @since 2025-02-21 09:34:02
 */
@RestController
@RequestMapping("/resourceInfo")
@Tag(name = "资源中心")
@CrossOrigin(origins = "*")
public class ResourceInfoController {

    @Resource
    private ResourceInfoService resourceInfoService;

    @PostMapping("/list")
    @AuditLog(module = "资源中心", auditObject = "资源", operation = "listQuery", message = "分页查询资源列表")
    public R<Page<ResourceInfo>> queryByPage(@RequestBody ResourceInfo resourceInfo) {
        Map<String, Map<String, String>> extendData = new LinkedHashMap<String, Map<String, String>>();
        Map<String, String> condition = new LinkedHashMap<String, String>();
        condition.put("资源名称", resourceInfo.getResourceName());
        condition.put("资源类型", resourceInfo.getResourceType());
        extendData.put("查询条件", condition);
        AuditExtendContext.setExtendData((Map) extendData);
        return R.ok(resourceInfoService.queryByPage(resourceInfo));
    }

    @GetMapping("/detail/{id}")
    @AuditLog(module = "资源中心", auditObject = "资源", operation = "detailsQuery", message = "查询资源详情")
    public R<ResourceInfo> queryById(@PathVariable("id") Long id) {
        GeneralMessage msg = new GeneralMessage();
        msg.setDataType("资源");
        msg.setDataName("资源ID: " + id);
        msg.setUniqueIdentification(Collections.singletonList(String.valueOf(id)));
        msg.setOperationNum(1);
        AuditExtendContext.setExtendData(toMap(msg));
        return R.ok(resourceInfoService.queryById(id));
    }

    @PostMapping("/create")
    @AuditLog(module = "资源中心", auditObject = "资源", operation = "create", message = "新增资源")
    public R<ResourceInfo> add(@RequestBody ResourceInfo resourceInfo) {
        // 插入数据（会回填主键）
        resourceInfoService.insert(resourceInfo);

        // 构建扩展审计信息
        GeneralMessage msg = new GeneralMessage();
        msg.setDataType("资源");
        msg.setDataName(resourceInfo.getResourceName());
        msg.setUniqueIdentification(Collections.singletonList(String.valueOf(resourceInfo.getId()))); // 回填后的ID
        msg.setOperationNum(1);
        AuditExtendContext.setExtendData(toMap(msg));

        return R.ok(resourceInfo);
    }


    @PostMapping("/update")
    @AuditLog(module = "资源中心", auditObject = "资源", operation = "update", message = "更新资源")
    public R<ResourceInfo> edit(@RequestBody ResourceInfo resourceInfo) {
        GeneralMessage msg = new GeneralMessage();
        msg.setDataType("资源");
        msg.setDataName(resourceInfo.getResourceName());
        msg.setUniqueIdentification(Collections.singletonList(resourceInfo.getResourceId()));
        msg.setOperationNum(1);
        AuditExtendContext.setExtendData(toMap(msg));
        return R.ok(resourceInfoService.update(resourceInfo));
    }

    @DeleteMapping("/delete/{id}")
    @AuditLog(module = "资源中心", auditObject = "资源", operation = "delete", message = "删除资源")
    public R<Boolean> deleteById(@PathVariable("id") Long id) {
        GeneralMessage msg = new GeneralMessage();
        msg.setDataType("资源");
        msg.setDataName("资源ID: " + id);
        msg.setUniqueIdentification(Collections.singletonList(String.valueOf(id)));
        msg.setOperationNum(1);
        AuditExtendContext.setExtendData(toMap(msg));
        return R.ok(resourceInfoService.deleteById(id));
    }

    private Map<String, Object> toMap(Object obj) {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.convertValue(obj, Map.class);
    }
}
